package de.tabuma.soapforms.soapproject.controller;

import de.tabuma.soapforms.*;
import de.tabuma.soapforms.soapproject.entity.*;
import de.tabuma.soapforms.soapproject.logik.*;
import de.tabuma.lib.tabumacontroller;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import de.tabuma.soap.WsdlFactory;
import de.tabuma.soap.logik.*;
import de.tabuma.soap.entity.wsdl.*;
import de.tabuma.soap.entity.soap.*;

/**
 *
 * @author alefen
 */
public class update extends tabumacontroller {

    private static final Logger log = Logger.getLogger(update.class.getName());

    public update(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        if(request.getParameter(constants.cancel)!=null) {
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String soapProjectId = request.getParameter(constants.soapProjectId);
        int soapProjectIdInt = Integer.valueOf(soapProjectId).intValue();

        if(request.getParameter(constants.updateSoapProject)!=null) {
            SoapProject sp = SoapProjectFacade.get(soapProjectIdInt);

            TDefinitions wsdl = de.tabuma.soap.logik.PMF.get(sp.getWsdlId(), new TDefinitions());
            List<Long> operationList1 = SoapFacade.getAllOperation(wsdl);
            List<String> messageList = new ArrayList<String>();
            for(int i=0;i<operationList1.size();i++) {
                TOperation operation = PMF.get(operationList1.get(i), new TOperation());
                messageList.add(operation.getName());
            }

            request.setAttribute(constants.updateSoapProject, "true");
            request.setAttribute(constants.soapProjectId, ""+soapProjectId);
            request.setAttribute(constants.soapProject, sp);
            request.setAttribute(constants.soapProjectMethodList, messageList);
        }

        if(request.getParameter(constants.updateSoapProject1)!=null) {
            String method = request.getParameter(constants.soapProjectMethod);
            String name = request.getParameter(constants.soapProjectName);
            String customer = request.getParameter(constants.customerId);
            int customerId = Integer.valueOf(customer).intValue();
            SoapProject soapProject = SoapProjectFacade.get(soapProjectIdInt);
            soapProject.setMethod(method);
            soapProject.setName(name);
            soapProject.setCustomerId(customerId);
            SoapProjectFacade.update(soapProject, user.getEmail());

            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
        }
    }
}
